//
//  LFUIImageWaterMark.swift
//  webApp
//
//  Created by Mical_LF on 2017/6/9.
//  Copyright © 2017年 Mical_LF. All rights reserved.
//

import Foundation
import UIKit

extension UIImage {
    /**
     *  给图片加水印图片
     *
     *  @param image   水印图片
     *  @param imgRect 水印图片所在位置，大小
     *  @param alpha   水印图片的透明度，0~1之间，透明度太大会完全遮盖被加水印图片的那一部分
     *
     *  @return 加完水印的图片
     */
    func imageWaterMarkOnlyImage(image: UIImage, imageRect: CGRect, alpha: CGFloat) -> UIImage{
        return self.imageWaterMarkRect(str: nil, strRect: CGRect.zero, attri: nil, image: image, imageRect: imageRect, alpha: alpha)
    }
    
    /**
     *  同上
     *
     *  @param image    同上
     *  @param imgPoint 水印图片（0，0）所在位置
     *  @param alpha    同上
     *
     *  @return 同上
     */
    func imageWaterMarkWithImage(image: UIImage, str: NSString?, imagePoint: CGPoint, alpha: CGFloat) -> UIImage {
        return self.imageWaterMarkPoint(str: nil, strPoint: CGPoint.zero, attri: nil, image: image, imagePoint: imagePoint, alpha: alpha)
    }
    
    /**
     *  给图片加文字水印
     *
     *  @param str     水印文字
     *  @param strRect 文字所在的位置大小
     *  @param attri   文字的相关属性，自行设置
     *
     *  @return 加完水印文字的图片
     */
    func imageWaterMarkWithString(str: NSString?, strRect: CGRect, attri: NSDictionary?) -> UIImage {
        return self.imageWaterMarkRect(str: str, strRect: strRect, attri: attri, image: nil, imageRect: CGRect.zero, alpha: 0)
    }
    
    /**
     *  同上
     *
     *  @param str      同上
     *  @param strPoint 文字（0，0）点所在位置
     *  @param attri    同上
     *
     *  @return 同上
     */
    func imageWaterMarkWithString(str: NSString?, strPoint: CGPoint, attri: NSDictionary?) -> UIImage {
        return self.imageWaterMarkPoint(str: str, strPoint: strPoint, attri: attri, image: nil, imagePoint: CGPoint.zero, alpha: 0)
    }
    
    
    /**
     *  返回加水印文字和图片的图片
     *
     *  @param str      水印文字
     *  @param strPoint 文字（0，0）点所在位置
     *  @param attri    文字属性
     *  @param image    水印图片
     *  @param imgPoint 图片（0，0）点所在位置
     *  @param alpha    透明度
     *
     *  @return 加完水印的图片
     */
    func imageWaterMarkPoint(str: NSString?, strPoint: CGPoint, attri: NSDictionary?, image: UIImage?, imagePoint: CGPoint, alpha: CGFloat) -> UIImage {
        UIGraphicsBeginImageContext(self.size)
        self.draw(at: CGPoint(x:0, y:0), blendMode: CGBlendMode.normal, alpha:1.0)
        if image != nil {
            image?.draw(at: imagePoint, blendMode: CGBlendMode.normal, alpha: alpha)
        }
        if (str?.boolValue)! {
            str?.draw(at: strPoint, withAttributes:attri as? [String : Any])
        }
        let resultImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return resultImage!
    }
    /**
     *  同上
     *
     *  @param str     同上
     *  @param strRect 文字的位置大小
     *  @param attri   同上
     *  @param image   同上
     *  @param imgRect 图片的位置大小
     *  @param alpha   透明度
     *
     *  @return 同上
     */
    func imageWaterMarkRect(str: NSString?, strRect: CGRect, attri: NSDictionary?, image: UIImage?, imageRect: CGRect, alpha: CGFloat) -> UIImage {
        let imagesize = self.size
        UIGraphicsBeginImageContext(imagesize)
        self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height), blendMode: CGBlendMode.normal, alpha: alpha)
        if (image != nil){
            image?.draw(in: imageRect, blendMode: CGBlendMode.normal, alpha: alpha)
        }
        
        if (str?.boolValue)! {
            str?.draw(in: strRect, withAttributes: attri as? [String : Any])
        }
        
        let resultImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return resultImage!
    }
    
    /**
     *  重设图片大小
     */    
    func newSizeImage(size: CGSize, source_image: UIImage) -> UIImage {
        var newSize = CGSize(width: source_image.size.width, height: source_image.size.height)
        let tempHeight = newSize.height / size.height
        let tempWidth = newSize.width / size.width
        
        if tempWidth > 1.0 && tempWidth > tempHeight {
            newSize = CGSize(width: source_image.size.width / tempWidth, height: source_image.size.height / tempWidth)
        } else if tempHeight > 1.0 && tempWidth < tempHeight {
            newSize = CGSize(width: source_image.size.width / tempHeight, height: source_image.size.height / tempHeight)
        }
        
        UIGraphicsBeginImageContext(newSize)
        source_image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
    }
}



extension UIView {
    
    
    func findController() -> UIViewController! {
        return self.findControllerWithClass(UIViewController.self)
    }
    
    func findNavigator() -> UINavigationController! {
        return self.findControllerWithClass(UINavigationController.self)
    }
    
    func findControllerWithClass<T>(_ clzz: AnyClass) -> T? {
        var responder = self.next
        while(responder != nil) {
            if (responder!.isKind(of: clzz)) {
                return responder as? T
            }
            responder = responder?.next
        }
        
        return nil
    }
    
    
}

